//
//  GCDBarrierTableViewController.m
//  GCDDemo
//
//  Created by 苏沫离 on 2018/8/5.
//  Copyright © 2018年 苏沫离. All rights reserved.
//
#define CellIdentifer @"UITableViewCell"

#import "GCDBarrierTableViewController.h"

@interface GCDBarrierTableViewController ()
@property (nonatomic ,strong) NSArray<NSString *> *titleArray;
@end

@implementation GCDBarrierTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer forIndexPath:indexPath];
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.textLabel.numberOfLines = 0;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"-------- 开始处理 --------");
    
    if (indexPath.row == 0)
    {
        [self dispatch_barrierSync_sync_Method];
    }
    else if (indexPath.row == 1)
    {
        [self dispatch_barrierSync_async_Method];
    }
    else if (indexPath.row == 2)
    {
        [self dispatch_barrierAsync_sync_Method];
    }
    else if (indexPath.row == 3)
    {
        [self dispatch_barrierAsync_async_Method];
    }
    else if (indexPath.row == 4)
    {

    }
    else if (indexPath.row == 5)
    {

    }
    else if (indexPath.row == 6)
    {

    }

    NSLog(@"-------- 结束处理 --------");
}

#pragma mark 同步栅栏 + 同步执行

- (void)dispatch_barrierSync_sync_Method
{
    dispatch_queue_t queue = dispatch_queue_create("com.demo.task", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        NSLog(@"开始执行taskA === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:1];//模拟耗时任务
        NSLog(@"结束执行taskA === %@",NSThread.currentThread);
    });
    dispatch_sync(queue, ^{
        NSLog(@"开始执行taskB === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:3];//模拟耗时任务
        NSLog(@"结束执行taskB === %@",NSThread.currentThread);
    });
    NSLog(@"开始栅栏");
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"开始执行taskC === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:2];//模拟耗时任务
        NSLog(@"结束执行taskC === %@",NSThread.currentThread);
    });
    
    dispatch_barrier_sync(queue, ^{
        NSLog(@"开始执行taskD === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:2];//模拟耗时任务
        NSLog(@"结束执行taskD === %@",NSThread.currentThread);
    });
    NSLog(@"结束栅栏");
    dispatch_sync(queue, ^{
        NSLog(@"开始执行taskE === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:4];//模拟耗时任务
        NSLog(@"结束执行taskE === %@",NSThread.currentThread);
    });
}

#pragma mark 同步栅栏 + 异步执行

- (void)dispatch_barrierSync_async_Method
{
    dispatch_queue_t queue = dispatch_queue_create("com.demo.task", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{
        NSLog(@"开始执行taskA === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:1];//模拟耗时任务
        NSLog(@"结束执行taskA === %@",NSThread.currentThread);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"开始执行taskB === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:3];//模拟耗时任务
        NSLog(@"结束执行taskB === %@",NSThread.currentThread);
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"开始执行taskC === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:2];//模拟耗时任务
        NSLog(@"结束执行taskC === %@",NSThread.currentThread);
    });

    NSLog(@"开始栅栏");
    dispatch_barrier_sync(queue, ^{
        NSLog(@"开始执行taskD === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:2];//模拟耗时任务
        NSLog(@"结束执行taskD === %@",NSThread.currentThread);
    });
    NSLog(@"结束栅栏");
    dispatch_async(queue, ^{
        NSLog(@"开始执行taskE === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:4];//模拟耗时任务
        NSLog(@"结束执行taskE === %@",NSThread.currentThread);
    });
}


#pragma mark 异步栅栏 + 同步执行

- (void)dispatch_barrierAsync_sync_Method
{
    dispatch_queue_t queue = dispatch_queue_create("com.demo.task", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_sync(queue, ^{
        NSLog(@"开始执行taskA === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:1];//模拟耗时任务
        NSLog(@"结束执行taskA === %@",NSThread.currentThread);
    });
    
    dispatch_sync(queue, ^{
        NSLog(@"开始执行taskB === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:3];//模拟耗时任务
        NSLog(@"结束执行taskB === %@",NSThread.currentThread);
    });
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"开始执行taskC === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:2];//模拟耗时任务
        NSLog(@"结束执行taskC === %@",NSThread.currentThread);
    });

    NSLog(@"开始栅栏");
    dispatch_barrier_async(queue, ^{
        NSLog(@"开始执行taskD === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:2];//模拟耗时任务
        NSLog(@"结束执行taskD === %@",NSThread.currentThread);

    });
    NSLog(@"结束栅栏");
    dispatch_sync(queue, ^{
        NSLog(@"开始执行taskE === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:4];//模拟耗时任务
        NSLog(@"结束执行taskE === %@",NSThread.currentThread);
    });
}



#pragma mark 异步栅栏 + 异步执行

- (void)dispatch_barrierAsync_async_Method
{
    dispatch_queue_t queue = dispatch_queue_create("com.demo.task", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        NSLog(@"开始执行taskA === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:1];//模拟耗时任务
        NSLog(@"结束执行taskA === %@",NSThread.currentThread);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"开始执行taskB === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:3];//模拟耗时任务
        NSLog(@"结束执行taskB === %@",NSThread.currentThread);
    });
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"开始执行taskC === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:2];//模拟耗时任务
        NSLog(@"结束执行taskC === %@",NSThread.currentThread);
    });
    NSLog(@"开始栅栏");
    dispatch_barrier_async(queue, ^{
        NSLog(@"开始执行taskD === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:2];//模拟耗时任务
        NSLog(@"结束执行taskD === %@",NSThread.currentThread);
    });
    NSLog(@"结束栅栏");

    dispatch_async(queue, ^{
        NSLog(@"开始执行taskE === %@",NSThread.currentThread);
        [NSThread sleepForTimeInterval:4];//模拟耗时任务
        NSLog(@"结束执行taskE === %@",NSThread.currentThread);

    });
}

#pragma mark - setter and getter

- (NSArray<NSString *> *)titleArray
{
    if (_titleArray == nil)
    {
        _titleArray = @[@"同步栅栏 + 同步执行",
                        @"同步栅栏 + 异步执行",
                        @"异步栅栏 + 同步执行",
                        @"异步栅栏 + 异步执行"];
    }
    return _titleArray;
}


@end
